use axum::{extract::Request, middleware::Next, response::Response};
use tracing::Instrument;

use crate::util;

pub async fn trace_id(request: Request, next: Next) -> Response {
    let uri = request.uri().to_string();
    let trace_id = util::uuid();
    async {
        let mut response = next.run(request).await;

        response
            .headers_mut()
            .append("trace_id", trace_id.parse().unwrap());

        response
    }
    .instrument(tracing::info_span!("request", trace_id, uri))
    .await
}
